

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta name="referrer" content="no-referrer" />
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/apple-touch-icon.png">
  <link rel="icon" href="/img/fyy.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="fyy">
  <meta name="keywords" content="">
  
    <meta name="description" content="实验一: 线程的创建与撤销一、实验目的一、实(1)熟悉windows系统提供的线程创建与撤销系统调用.(2)掌握windows系统环境下线程的创建与撤销方法. 二、准备1. 线程的创建CeateThread()完成线程的创建.它在调用进程的地址空间上创建一个线程,执行指定的函数,并返回新建立的线程的句柄.原型: 12345678HANDLE CeateThread(	LPSECURITY_ATTR">
<meta property="og:type" content="article">
<meta property="og:title" content="线程的创建与撤销 c++">
<meta property="og:url" content="http://example.com/2020/04/18/%E8%AF%BE%E7%A8%8B%E5%AE%9E%E9%AA%8C/%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%88%9B%E5%BB%BA%E4%B8%8E%E6%92%A4%E9%94%80%20c++/index.html">
<meta property="og:site_name" content="fyy-coding">
<meta property="og:description" content="实验一: 线程的创建与撤销一、实验目的一、实(1)熟悉windows系统提供的线程创建与撤销系统调用.(2)掌握windows系统环境下线程的创建与撤销方法. 二、准备1. 线程的创建CeateThread()完成线程的创建.它在调用进程的地址空间上创建一个线程,执行指定的函数,并返回新建立的线程的句柄.原型: 12345678HANDLE CeateThread(	LPSECURITY_ATTR">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/images/3a56ea2c-e9ac-4e39-8510-a4bf2277cdac.png">
<meta property="og:image" content="http://example.com/images/8b825336-db16-432a-af97-2a961f3c9dfc.png">
<meta property="og:image" content="http://example.com/images/a38b8e46-8195-4b9c-97a0-5476671a1daa.png">
<meta property="og:image" content="http://example.com/images/3febb261-ea76-43e1-8974-57491f16d10f.png">
<meta property="og:image" content="http://example.com/images/a2173715-7f80-4543-97a3-75fa46bb6377.png">
<meta property="article:published_time" content="2020-04-18T08:55:46.000Z">
<meta property="article:modified_time" content="2024-03-26T13:10:44.780Z">
<meta property="article:author" content="fyy">
<meta property="article:tag" content="c++">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="http://example.com/images/3a56ea2c-e9ac-4e39-8510-a4bf2277cdac.png">
  
  
  
  <title>线程的创建与撤销 c++ - fyy-coding</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.7","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"follow_dnt":true,"baidu":null,"google":{"measurement_id":null},"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":"ufph8TbmK43d1JfQxLvss4KY-MdYXbMMI","app_key":"96APiPOtZEiCQlv5h3jjZfbC","server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml","include_content_in_search":true};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  

  

  
    <!-- Google tag (gtag.js) -->
    <script async>
      if (!Fluid.ctx.dnt) {
        Fluid.utils.createScript("https://www.googletagmanager.com/gtag/js?id=", function() {
          window.dataLayer = window.dataLayer || [];
          function gtag() {
            dataLayer.push(arguments);
          }
          gtag('js', new Date());
          gtag('config', '');
        });
      }
    </script>
  

  

  

  

  
    
  



  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>fyy-coding</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/" target="_self">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/" target="_self">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/" target="_self">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/" target="_self">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/" target="_self">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="线程的创建与撤销 c++"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2020-04-18 16:55" pubdate>
          2020年4月18日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          1.3k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          11 分钟
        
      </span>
    

    
    
      
        <span id="leancloud-page-views-container" class="post-meta" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="leancloud-page-views"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <h1 id="seo-header">线程的创建与撤销 c++</h1>
            
            
              <div class="markdown-body">
                
                <h2 id="实验一-线程的创建与撤销"><a href="#实验一-线程的创建与撤销" class="headerlink" title="实验一: 线程的创建与撤销"></a>实验一: 线程的创建与撤销</h2><h3 id="一、实验目的"><a href="#一、实验目的" class="headerlink" title="一、实验目的"></a>一、实验目的</h3><h3 id="一、实"><a href="#一、实" class="headerlink" title="一、实"></a>一、实</h3><p>(1)熟悉windows系统提供的线程创建与撤销系统调用.<br>(2)掌握windows系统环境下线程的创建与撤销方法.</p>
<h3 id="二、准备"><a href="#二、准备" class="headerlink" title="二、准备"></a>二、准备</h3><h5 id="1-线程的创建"><a href="#1-线程的创建" class="headerlink" title="1. 线程的创建"></a>1. 线程的创建</h5><p>CeateThread()完成线程的创建.它在调用进程的地址空间上创建一个线程,执行指定的函数,并返回新建立的线程的句柄.<br>原型:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-variable constant_">HANDLE</span> <span class="hljs-title class_">CeateThread</span>(<br>	<span class="hljs-variable constant_">LPSECURITY_ATTRIBUTES</span> lpThreadAttributes,<br>	<span class="hljs-variable constant_">DWORD</span> dwStackSize,<br>	<span class="hljs-variable constant_">LPSECURITY_START_ROUTINE</span> lpStartAddress,<br>	<span class="hljs-variable constant_">LPVOID</span> lpparameter,<br>	<span class="hljs-variable constant_">DWORD</span> dwCreationFlags,<br>	<span class="hljs-variable constant_">LPDWORD</span> lpThreadId<br>);<br></code></pre></td></tr></table></figure>

<p>参数说明:</p>
<ol>
<li>lpThreadAttributes:为线程指定安全属性.为NULL时,线程得到一个默认的安全描述符.</li>
<li>dwStackSize:线程堆栈的大小.其值为0时,其大小与调用该线程的线程堆栈大小相同.</li>
<li>lpStartAddress:指定线程要执行的函数.</li>
<li>lpparameter:函数中要传递的参数.</li>
<li>dwCreationFlags:指定线程创建后所处的状态.若为CRRATE_SUSPENDED,表示创建后出于挂起状态,用ResumeThread()激活线程才可以执行.若该值为0，表示线程创建后立即执行.</li>
<li>lpThreadId:用一个32位的变量接受系统返回的线程标识符.若该值设为NULL,系统不返回线程标识符.<br>返回值:<br>如果线程创建成功,将返回线程的句柄;如果失败,系统返回NULL,可以调用函数GetLastError查询失败的原因.<br>用法举例:<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">static</span> <span class="hljs-variable constant_">HANDLE</span> hHandle1=<span class="hljs-variable constant_">NULL</span>; <span class="hljs-comment">//用于存储线程返回句柄的变量</span><br><span class="hljs-variable constant_">DWORD</span> dwThreadID1;           <span class="hljs-comment">//用于存储线程标识符的变量</span><br><span class="hljs-comment">//创建一个名为ThreadName1的线程</span><br>hHandle1=<span class="hljs-title class_">CeateThread</span>((<span class="hljs-variable constant_">LPSECURITY_ATTRIBUTES</span>)) <span class="hljs-variable constant_">NULL</span><br>                     <span class="hljs-number">0</span>,<br>					 (<span class="hljs-variable constant_">LPSECURITY_START_ROUTINE</span>)<span class="hljs-title class_">ThreadName1</span>,<br>					 (<span class="hljs-variable constant_">LPDWORD</span>)<span class="hljs-variable constant_">NULL</span>,<br>					 <span class="hljs-number">0</span>,&amp;dwThreadID1);<br></code></pre></td></tr></table></figure></li>
</ol>
<h5 id="2-撤销线程"><a href="#2-撤销线程" class="headerlink" title="2. 撤销线程"></a>2. 撤销线程</h5><p>ExitThread()用于撤销当前进程.<br>原型:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-variable constant_">VOID</span> <span class="hljs-title class_">ExitThread</span>(<br><span class="hljs-variable constant_">DWORD</span> dwExitCode);   <span class="hljs-comment">//线程返回码</span><br></code></pre></td></tr></table></figure>

<p>参数说明:<br>dwExitCode:指定线程返回码,可以调用GetExitCodeThread()查询返回码的含义.<br>返回值:<br>该函数没有返回值.<br>用法举例:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-title class_">ExitThread</span>(<span class="hljs-number">0</span>);<br></code></pre></td></tr></table></figure>

<h5 id="3-终止线程"><a href="#3-终止线程" class="headerlink" title="3.终止线程"></a>3.终止线程</h5><p>TerminateThread()用于终止当前线程.该函数与ExitThread()的区别在于,ExitThread()在撤销线程时将该线程所拥有的资源全部归还给系统,而TerminateThread()不归还资源.<br>原型:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-variable constant_">BOOL</span> <span class="hljs-title class_">TerminateThread</span>(<br><span class="hljs-variable constant_">HANDLE</span> hHandle,       <span class="hljs-comment">//线程句柄</span><br><span class="hljs-variable constant_">DWORD</span> dwExitCode);    <span class="hljs-comment">//线程返回码</span><br></code></pre></td></tr></table></figure>

<p>参数说明:<br>(1)hThread:要终止线程的线程句柄.<br>(2)dwExitCode:指定线程返回码,可以调用GetExitCodeThread()查询返回码的含义.<br>返回值:<br>函数调用成功,将返回一个非0值;若失败,返回0，可以调用函数GetLastError()查询失败的原因.</p>
<h5 id="4-挂起线程"><a href="#4-挂起线程" class="headerlink" title="4.挂起线程"></a>4.挂起线程</h5><p>Sleep()用于挂起当前正在执行的线程.<br>原型:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-variable constant_">VOID</span> <span class="hljs-title class_">Sleep</span>(<span class="hljs-variable constant_">DWORD</span> dwMilliseconds);<br></code></pre></td></tr></table></figure>

<p>参数说明:<br>dwMilliseconds;指定挂起时间,单位为ms(毫秒).<br>返回值:<br>该函数没有返回值.</p>
<h5 id="5-关闭句柄"><a href="#5-关闭句柄" class="headerlink" title="5.关闭句柄"></a>5.关闭句柄</h5><p>函数CloseHandle()用于关闭已打开的对象的句柄,其作用与释放动态申请的内存空间类似,这样可以释放系统资源,使线程安全运行.<br>原型:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-variable constant_">BOOL</span> <span class="hljs-title class_">CloseHandle</span>(<span class="hljs-variable constant_">HANDLE</span> hObject);<br></code></pre></td></tr></table></figure>

<p>参数说明:<br>hObject:已打开对象的句柄.<br>返回值:<br>如果函数调用成功,则返回值为非0值;如果函数调用失败,则返回值为0.若要得到更多的错误信息,调用函数GetLastError()查询.</p>
<h3 id="三、内容"><a href="#三、内容" class="headerlink" title="三、内容"></a>三、内容</h3><p>使用系统调用CreatThread()创建一个子线程,并在子线程中显示;Thread is Running!.为了能让用户清楚地看到线程的运行情况,使用Sleep()使线程挂起5s,之后使用ExitThread(0)撤销进程.</p>
<h4 id="要求"><a href="#要求" class="headerlink" title="要求"></a>要求</h4><p>能正确使用CreatThread(),ExitThread()及Sleep()等系统调用,进一步理解进程与线程理论.</p>
<h4 id="指导"><a href="#指导" class="headerlink" title="指导"></a>指导</h4><p>本实验在WindowsXP,Microsoft Visual C++ 6.0环境下实现,利用Windows SDK提供的API完成程序的功能.实验在Windows XP环境下安装由于WindowsXP,Microsoft Visual C++ 6.0是一个集成开发环境,其中包含了Windows SDK 所有工具和定义,所以安装了WindowsXP,Microsoft Visual C++ 6.0后不用特意安装SDK.试验中所有的API是操作系统提供的用来进行应用程序开发的系统功能接口.</p>
<ol>
<li>首先启动安装好的,Microsoft Visual C++ 6.0.</li>
<li>在,Microsoft Visual C++ 6.0环境下选择File-&gt;new命令,然后在Project选项卡中选择Win32 Console Application建立一个控制台工程文件.</li>
<li>由于CreatThread()等函数是Microsoft Windows操作系统的系统调用,因此,在下图中选择An application that supports MFC,之后单击Finish按钮.</li>
</ol>
<p><img src="/../images/3a56ea2c-e9ac-4e39-8510-a4bf2277cdac.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<ol>
<li>创建一个单线程操作并观看结果<br><img src="/../images/8b825336-db16-432a-af97-2a961f3c9dfc.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><img src="/../images/a38b8e46-8195-4b9c-97a0-5476671a1daa.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></li>
<li>创建一个多线程操作，并观看结果<br><img src="/../images/3febb261-ea76-43e1-8974-57491f16d10f.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><img src="/../images/a2173715-7f80-4543-97a3-75fa46bb6377.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></li>
</ol>
<h4 id="源程序"><a href="#源程序" class="headerlink" title="源程序"></a>源程序</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// test1.cpp : Defines the entry point for the console application.</span><br><span class="hljs-comment">//</span><br><br>#include <span class="hljs-string">&quot;stdafx.h&quot;</span><br>#include <span class="hljs-string">&quot;test1.h&quot;</span><br><br>#ifdef _DEBUG<br>#define <span class="hljs-keyword">new</span> <span class="hljs-variable constant_">DEBUG_NEW</span><br>#undef <span class="hljs-variable constant_">THIS_FILE</span><br><span class="hljs-keyword">static</span> char <span class="hljs-variable constant_">THIS_FILE</span>[] = __FILE__;<br>#endif<br><br>/<br><span class="hljs-comment">// The one and only application object</span><br><br><span class="hljs-title class_">CWinApp</span> theApp;<br><br>using namespace std;<br><br><br><br><span class="hljs-keyword">void</span> <span class="hljs-title function_">eatApple</span>(<span class="hljs-params">int apple_number</span>)<br>&#123;<br>    <br>	<span class="hljs-title class_">Sleep</span>((<span class="hljs-number">3</span>-apple_number)*<span class="hljs-number">1000</span>);<br>	<span class="hljs-title function_">printf</span>(<span class="hljs-string">&quot;i&#x27;m eating apple #%d.\n&quot;</span>, apple_number);<br>&#125;<br><br><br>int <span class="hljs-title function_">_tmain</span>(<span class="hljs-params">int argc, TCHAR* argv[], TCHAR* envp[]</span>)<br>&#123;<br>    <br>	<span class="hljs-variable constant_">HANDLE</span> handle1 = <span class="hljs-variable constant_">NULL</span>;<br>	<span class="hljs-variable constant_">HANDLE</span> handle2 = <span class="hljs-variable constant_">NULL</span>;<br>	<span class="hljs-variable constant_">HANDLE</span> handle3 = <span class="hljs-variable constant_">NULL</span>;<br>	<span class="hljs-variable constant_">DWORD</span> <span class="hljs-title class_">ThreadID1</span> = <span class="hljs-variable constant_">NULL</span>;<br>	<span class="hljs-variable constant_">DWORD</span> <span class="hljs-title class_">ThreadID2</span> = <span class="hljs-variable constant_">NULL</span>;<br>	<span class="hljs-variable constant_">DWORD</span> <span class="hljs-title class_">ThreadID3</span> = <span class="hljs-variable constant_">NULL</span>;<br><br>	int nRetCode;<br><br>	int a = <span class="hljs-number">0</span>;<br>	int b = <span class="hljs-number">1</span>;<br>	int c = <span class="hljs-number">2</span>;<br>	<br>	handle1 = <span class="hljs-title class_">CreateThread</span>((<span class="hljs-variable constant_">LPSECURITY_ATTRIBUTES</span>) <span class="hljs-variable constant_">NULL</span>,<br>		<span class="hljs-number">0</span>,<br>		(<span class="hljs-variable constant_">LPTHREAD_START_ROUTINE</span>) eatApple,<br>		(<span class="hljs-variable constant_">LPVOID</span>) a,<br>		<span class="hljs-number">0</span>,<br>		&amp;<span class="hljs-title class_">ThreadID1</span>);<br>	<br>	handle2 = <span class="hljs-title class_">CreateThread</span>((<span class="hljs-variable constant_">LPSECURITY_ATTRIBUTES</span>) <span class="hljs-variable constant_">NULL</span>,<br>		<span class="hljs-number">0</span>,<br>		(<span class="hljs-variable constant_">LPTHREAD_START_ROUTINE</span>) eatApple,<br>		(<span class="hljs-variable constant_">LPVOID</span>) b,<br>		<span class="hljs-number">0</span>,<br>		&amp;<span class="hljs-title class_">ThreadID2</span>);<br>	<br>	handle3 = <span class="hljs-title class_">CreateThread</span>((<span class="hljs-variable constant_">LPSECURITY_ATTRIBUTES</span>) <span class="hljs-variable constant_">NULL</span>,<br>		<span class="hljs-number">0</span>,<br>		(<span class="hljs-variable constant_">LPTHREAD_START_ROUTINE</span>) eatApple,<br>		(<span class="hljs-variable constant_">LPVOID</span>) c,<br>		<span class="hljs-number">0</span>,<br>		&amp;<span class="hljs-title class_">ThreadID3</span>);<br>	<span class="hljs-title class_">Sleep</span>(<span class="hljs-number">10000</span>);<br>	<span class="hljs-keyword">return</span> nRetCode;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="四、总结"><a href="#四、总结" class="headerlink" title="四、总结"></a>四、总结</h3><p>在Windows系统中进程是资源的拥有者,线程是系统调用的单位.进程创建后,其主线程也随即被创建.但是单线程只能执行完一个之后再执行另外一个线程，而多线程在一定程度上是多个线程一起执行的。</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E5%85%B6%E5%AE%83/" class="category-chain-item">其它</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/c/" class="print-no-link">#c++</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>线程的创建与撤销 c++</div>
      <div>http://example.com/2020/04/18/课程实验/线程的创建与撤销 c++/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>fyy</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2020年4月18日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a class="print-no-link" target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2020/04/29/%E8%AF%BE%E7%A8%8B%E5%AE%9E%E9%AA%8C/python%E7%88%AC%E5%8F%9651job%E7%9A%84%E6%8B%9B%E8%81%98%E4%BF%A1%E6%81%AF/" title="Python爬取51job的招聘信息">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Python爬取51job的招聘信息</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2020/04/18/%E8%AF%BE%E7%A8%8B%E5%AE%9E%E9%AA%8C/%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%90%8C%E6%AD%A5/" title="线程的同步 c++">
                        <span class="hidden-mobile">线程的同步 c++</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments" lazyload>
      
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://lib.baomitu.com/valine/1.5.1/Valine.min.js', function() {
        var options = Object.assign(
          {"appId":"ufph8TbmK43d1JfQxLvss4KY-MdYXbMMI","appKey":"96APiPOtZEiCQlv5h3jjZfbC","path":"window.location.pathname","placeholder":null,"avatar":"retro","meta":["nick","mail","link"],"requiredFields":[],"pageSize":10,"lang":"zh-CN","highlight":false,"recordIP":false,"serverURLs":"","emojiCDN":null,"emojiMaps":null,"enableQQ":false},
          {
            el: "#valine",
            path: window.location.pathname
          }
        )
        new Valine(options);
        Fluid.utils.waitElementVisible('#valine .vcontent', () => {
          var imgSelector = '#valine .vcontent img:not(.vemoji)';
          Fluid.plugins.imageCaption(imgSelector);
          Fluid.plugins.fancyBox(imgSelector);
        })
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="leancloud-site-pv-container" style="display: none">
        总访问量
        <span id="leancloud-site-pv"></span>
        次
      </span>
    
    
      <span id="leancloud-site-uv-container" style="display: none">
        总访客数
        <span id="leancloud-site-uv"></span>
        人
      </span>
    
    

  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.4/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.20.1/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script defer src="/js/leancloud.js" ></script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
